package com.cebbank.yyi.springbootapp.controller;

import com.cebbank.yyi.springbootapp.bo.*;
import com.cebbank.yyi.springbootapp.poi.ExcelUtils;
import com.cebbank.yyi.springbootapp.service.impl.ExcelServiceImpl;
import com.cebbank.yyi.springbootapp.tables.CreateTablesBO;
import com.cebbank.yyi.springbootapp.tables.ExcelFormatBO;
import com.cebbank.yyi.springbootapp.tables.ExportDataBO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: yyi
 * @version: 1.0
 **/

@RestController
@RequestMapping("/poi")
@RequiredArgsConstructor
public class PoiExcelController {

    private final static String SUCCESS = "SUCCESS";

    private final ExcelServiceImpl excelService;


    private final ApplicationContext applicationContext;

    /**
     * @description: 获取当前数据库中所有的数据表
     * @param:
     * @return:
     * @author: yyi
     * @version: 1.0
     **/
    @PostMapping("/webapi/getTables")
    public List<String> getTables() {
        return excelService.getAllTables();
    }

    /**
     * 导入Excel
     * 对应数据库中没有表结构的场景
     * 1 包含创建表
     * 2 插入数据
     **/
    @PostMapping("/webapi/import")
    public List<CreateTablesBO> importData(MultipartFile file) throws Exception {
        return excelService.importExcel(file.getInputStream());
    }

    @PostMapping("/webapi/execute")
    public String executeSql(@RequestBody DealBO dealBO) {
        excelService.executeSql(dealBO.getSql());
        return SUCCESS;
    }

    /**
     * @description: 支持提供外传sql, 对已经导入的数据使用sql进行过滤 包括并不限定于求和 求平均数等
     * @param:
     * @return:
     * @author: yyi
     * @version: 1.0
     **/
    @PostMapping("/webapi/get")
    public String refreshData(@RequestBody ExportDataBO sqlBo) {
        List<HashMap<String, Object>> stringObjectMap = excelService.executeSql(sqlBo.getSql());
        excelService.dataToExcel(stringObjectMap, sqlBo, "D:\\code\\a1.xlsx");
        return SUCCESS;
    }

    /**
     * 合并同类项的单元格 并且居中显示
     * **/
    @PostMapping("/webapi/merge")
    public void merge(@RequestPart("file") MultipartFile file, @RequestPart("format") ExcelFormatBO format) throws IOException {
        excelService.mergeCell(file.getInputStream(), "D:\\code\\a12.xlsx", 2, format);
    }


    /**
     * 合并同类项的单元格 并且居中显示
     * **/
    @PostMapping("/webapi/merge/column")
    public String mergeColumn(@RequestPart("file") MultipartFile file, @RequestPart("merge") MergeCellBO mergeCellBO) throws IOException {
        excelService.mergeSameCellsInColumn(file.getInputStream(), mergeCellBO, "D:\\code\\a12qqqq.xlsx");
        return SUCCESS;
    }



    /**
     * 支持上传多个文件
     * 导入数据局库
     * **/
    @PostMapping("/webapi/merge/all")
    public List<CreateTablesBO> mergeExcelAll(@RequestPart("file") List<MultipartFile> files) throws Exception {
        return excelService.mergeAllExcels(files);
    }


    @PostMapping("/webapi/import1")
    public void importData1(MultipartFile file) {
        List<Class<?>> clazz = new ArrayList<>();
        clazz.add(UserBO.class);
        clazz.add(EmployeeBO.class);
        ExcelUtils<Object> excelUtil = new ExcelUtils<>(clazz);
        try {
            List<List<Object>> lists = excelUtil.importExcels(file.getInputStream(), 1);
            System.err.println(lists);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 获取指定表结构的字段
     * 提供导出指定列
     **/
    @PostMapping("/webapi/getTableInfo")
    public List<String> exportExcel(@RequestBody TableInfoBO table) {
        return excelService.getTableFields(table.getTableName());
    }

    /**
     * 导出指定列 -> excel
     **/
    @PostMapping("/webapi/exportFiled")
    public String exportExcelFields(@RequestBody ExportFiledsBO exportFiledsBO) {
        excelService.exportExcelFields(exportFiledsBO);
        return SUCCESS;
    }

    @PostMapping("/webapi/exportFiled/split")
    public String exportExcelFieldsSplit(@RequestBody ExportFiledExtendBO exportFiledsBO) {
        excelService.exportExcelFieldsSplit(exportFiledsBO);
        return SUCCESS;
    }



    /***
     * 导出多个表 到同一个文件的不同sheet页
     * **/
    @PostMapping("/webapi/exportSheet")
    public String exportExcelSheet(@RequestBody List<com.cebbank.yyi.springbootapp.tables.TableInfoBO> names) {
        excelService.exportExcelSheet(names);

        return SUCCESS;
    }

    /***
     * 根据模板导出Excel
     * **/
    @PostMapping("/webapi/exportExcelByTemplate")
    public String exportExcelByTemplate(@RequestBody HashMap<String, Object> map) {
        excelService.exportExcelByTemplate(map, "D:\\code\\example.xlsx");
        return SUCCESS;
    }


    /***
     * 对应一个需求，读取excel, 并支持使用sql对数据进行清洗
     * **/
    @PostMapping("/webapi/import/table")
    public String  readExcel(@RequestPart("file") MultipartFile file, @RequestPart("data") DealBO dealBO) throws IOException {
        excelService.readExcel(file.getInputStream(), dealBO.getSql(), dealBO.getNum());
        return SUCCESS;
    }


    /***
     * 对应一个需求，读取excel, 并支持使用sql对数据进行清洗
     * **/
    @PostMapping("/webapi/split/param")
    public String  splitByParam(@RequestPart("file") MultipartFile file, @RequestPart("split") SplitFileBO list) throws Exception {
        excelService.splitByParam(file.getInputStream(), list);
        return SUCCESS;
    }


}
